package com.ms.qw.domain.bean;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * User: Matrix
 * Date: 23/01/13
 */
public class ExcelProperties {
    private static final Logger LOG = LoggerFactory.getLogger(ExcelProperties.class);

    List<CeldaInfo> celdasInfo = new ArrayList<CeldaInfo>();

    List indexColumnas = new ArrayList();
    List nombreColumas = new ArrayList();

    public ExcelProperties(List<CeldaInfo> celdasInfo) {
        this.celdasInfo = celdasInfo;
        for (CeldaInfo celdaInfo : celdasInfo) {
            indexColumnas.add(celdaInfo.getColumnaIndex());
            nombreColumas.add(celdaInfo.getNombre());
        }
    }

    public boolean esValido(int colIdx,Cell myCell) {
        boolean result = indexColumnas.contains(colIdx);
        if(result){
            if(myCell==null) return true;
            if (myCell.getCellType() == Cell.CELL_TYPE_STRING) {
                if (nombreColumas.contains(myCell.getStringCellValue().trim())) {
                    result = false;
                }
            }
        }

        return result;
    }

    public void init(File archivoOrigen) {
        try {
            FileInputStream myInput = new FileInputStream(archivoOrigen);

            POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);

            HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);

            HSSFSheet mySheet = myWorkBook.getSheetAt(0);

            Iterator rowIter = mySheet.rowIterator();

            while (rowIter.hasNext()) {
                HSSFRow myRow = (HSSFRow) rowIter.next();
                Iterator cellIter = myRow.cellIterator();
                boolean tituloEncontrado = false;
                while (cellIter.hasNext()) {
                    HSSFCell myCell = (HSSFCell) cellIter.next();
                    if (celdasInfo.contains(new CeldaInfo(-1,myCell.getStringCellValue()))) {
                        tituloEncontrado = true;
                        CeldaInfo celdaInfo = celdasInfo.get(celdasInfo.indexOf(new CeldaInfo(-1,myCell.getStringCellValue())));
                        celdaInfo.setColumnaIndex(myCell.getColumnIndex());
                    }
                }
                if (tituloEncontrado) {
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public CeldaInfo getFormatIndex(int colIdx) {
        for (CeldaInfo celdaInfo : celdasInfo) {
            if (celdaInfo.getColumnaIndex() == colIdx) {
                return celdaInfo;
            }
        }
        return null;
    }

    public List<CeldaInfo> getCeldasInfo() {
        return celdasInfo;
    }
}
